package io.github.tangwan.utils;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @author tangwan
 * @version V1.0
 * created on 2021/10/20 13:40
 * @since JDK 1.8
 */
public abstract class CommonUtil {

    public static Pattern MOBILE_PATTERN = Pattern.compile("(^1[3|4|5|6|7|8|9]\\d{9}$)");

    /**
     * 是否是手机号
     * @param mobile
     * @return
     */
    public static boolean isMobile(String mobile) {
        return MOBILE_PATTERN.matcher(mobile).matches();
    }

    /**
     * 把list均匀拆分成指定片数(Guava Lists.partition())
     * @param source 待拆分list
     * @param n 多少片
     * @param <T>
     * @return list=>list=>T
     */
    public static <T> List<List<T>> averageAssign(List<T> source, int n) {
        List<List<T>> result = new ArrayList<List<T>>();
        //(先计算出余数)
        int remaider = source.size() % n;
        //然后是商
        int number = source.size() / n;
        //偏移量
        int offset = 0;
        for (int i = 0; i < n; i++) {
            List<T> value = null;
            if (remaider > 0) {
                value = source.subList(i * number + offset, (i + 1) * number + offset + 1);
                remaider--;
                offset++;
            } else {
                value = source.subList(i * number + offset, (i + 1) * number + offset);
            }
            result.add(value);
        }
        return result;
    }

}
